import React from 'react';
import {connect} from 'dva/index';
import {Button, Col, Form, Input, Popconfirm, Row, Switch, Table} from 'antd';
import Constants from '../../utils/Constants';
import RightConstants from '../../utils/RightConstants';
import RightBox from '../../components/common/rightBox';
import DeclareProfitDetail from '../../components/profit/profit/declareProfitDetail';

const FormItem = Form.Item;

const declareProfit = Form.create()(class Profit extends React.Component {
    constructor(props) {
        super(props);
    }

    componentDidMount() {
        this.props.dispatch({ type: 'profit/getListForPage',
            payload: { page: Constants.DEFAULT_PAGE ,type: Constants.TYPE2 }
    });
    }
    onSwitch = (id,checked) => {
        this.props.dispatch({
            type: 'profit/updateProfitStatus',
            payload: {
                id,
                status: checked ? "1" : "0",
                onSuccess: this.handleSearch
            }
        })
    }
    handleOpen = (id, disabled) => {
        this.props.dispatch({
            type: 'profit/getProfitById',
            payload: { id
            }
        });
        this.props.dispatch({ type: 'supplier/getSupplierList',
            payload: { supplierType: Constants.SUPPLIER_TYPE.DECLARE ,type: Constants.TYPE2} ,
        });  //查询报关供应商

        this.declareProfitDetail.handleOpen(disabled);
    }

    handleDelete = (id) => {
        this.props.dispatch({
            type: 'profit/deleteProfitById',
            payload: {
                id,
                // status: checked ? "1" : "0",
                onSuccess: this.handleSearch
            }
        })
    }

    handleSearch = flag => {
        let newPage = this.props.profit.page;
        if (flag) newPage.page = 1;
        this.props.dispatch({
            type: 'profit/getListForPage',
            payload: {
                ...this.props.form.getFieldsValue(),
                type: Constants.TYPE2,
                page: newPage
            }
        });
    };

    columns = [
        { title: '供应商代码', dataIndex: 'supplierCode', key: 'supplierCode' },
        { title: '供应商名称', dataIndex: 'companyname', key: 'companyname  ' },
        { title: '有效开始时间', dataIndex: 'begindate', key: 'begindate' },
        { title: '有效截止时间', dataIndex: 'enddate', key: 'enddate' },
        {
            title: '启用/停用',
            render: (record) => {
                return (
                    <Switch checkedChildren={'启用'} unCheckedChildren={'停用'} checked={record.status === "1"} onChange={this.onSwitch.bind(this, record.id)} />
                )
            }
        },
        { title: '备注', dataIndex: 'memo', key: 'memo' },
        {
            title: '操作',
            render: (record) => {
                let editLink = <RightBox resource={RightConstants.OPERATIONSETTING.OPERATIONSETTING_DECLAREPROFIT_EDIT} split={true}><a href="#" onClick={this.handleOpen.bind(this, record.id, false)}>编辑</a></RightBox>
                let deleteLink = <Popconfirm onConfirm={this.handleDelete.bind(this, record.id)} okText="是" cancelText="否"
                    placement="leftTop" title={'数据删除后将不可以恢复，是否继续删除?'}>
                    <a href="#">删除</a>
                </Popconfirm>
                deleteLink = <RightBox resource={RightConstants.OPERATIONSETTING.OPERATIONSETTING_DECLAREPROFIT_DELETE} split={true}>{deleteLink}</RightBox>
                let viewLink = <a href="#" onClick={this.handleOpen.bind(this, record.id, true)}>查看</a>
                return <span>{editLink}{deleteLink}{viewLink}</span>
            }
        }
    ];

    render() {
        let formItemLayout = { labelCol: { span: 8 }, wrapperCol: { span: 16 } };
        const { getFieldDecorator } = this.props.form;
        let pagination = {
            showTotal: total => `共 ${total} 条数据`,
            pageSize: this.props.profit.page.size,
            current: this.props.profit.page.page,
            total: this.props.profit.page.totalRow,
            showSizeChanger: true,
            onShowSizeChange: (current, size) => {
                let newPage = this.props.profit.page;
                newPage.size = size;
                newPage.page = 1;
                this.props.dispatch({
                    type: 'profit/getListForPage',
                    payload: {
                        ...this.props.form.getFieldsValue(),
                        page: newPage
                    }
                });
            },
            onChange: (pageNumber, pageSize) => {
                let newPage = this.props.profit.page;
                newPage.page = pageNumber;
                this.props.dispatch({
                    type: 'profit/getListForPage',
                    payload: {
                        ...this.props.form.getFieldsValue(),
                        page: newPage
                    }
                });
            }
        }
        return (
            <div>
                <Row>
                    <Col span={8}>
                        <FormItem {...formItemLayout} label={'供应商'}>
                            {getFieldDecorator('supplier')
                                (
                                <Input maxLength="20" placeholder="可根据供应商代码、中英文名称搜索" />
                                )
                            }
                        </FormItem>
                    </Col>
                    <Col span={8} style={{ marginLeft: 24 }}>
                        <Button type="primary" icon="search" style={{ marginRight: 5 }} onClick={this.handleSearch.bind(this, true)} size={'default'}>查询</Button>
                        <RightBox resource={RightConstants.OPERATIONSETTING.OPERATIONSETTING_DECLAREPROFIT_ADD}>
                            <Button type="primary" icon="plus" style={{ marginRight: 5 }} onClick={this.handleOpen.bind(this, null, false)} size={'default'}>新增报关利润</Button>
                        </RightBox>
                    </Col>
                </Row>
                <Table loading={this.props.loading} rowKey='id'  dataSource={this.props.profit.list} columns={this.columns} bordered={true} size={'middle'} pagination={pagination} />
                <DeclareProfitDetail wrappedComponentRef={(inst) => this.declareProfitDetail = inst} refreshList={this.handleSearch} />
            </div>
        );
    }
})

function mapStateToProps(state) {
    return {
        loading: state.loading.models.profit, declareProfit: state.declareProfit,profit: state.profit
    };
}

export default connect(mapStateToProps)(declareProfit);


